// pages/community/community.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    discussList:[],
    title:'',
    content:'',
    pageSum:'',
    pageIndex:1,
    order:0,
    isShow:false,
    isView:true,
    unFold:false,
    sortWay:"默认",
    array:["默认","最热","最近"]
  },

  getComment(){
    this.setData({
      isShow:true
    })
    wx.request({
      url: 'http://124.222.95.170:21311/discuss/page',
      method:'GET',
      header: {
        'Cookie': wx.getStorageSync('sessionid')
      },
      data:{
        "page":this.data.pageIndex,
        "pageSize":4,
        "sortType":this.data.order
      },
      success:(res)=>{
        console.log(res)
        if(res.data.msg == "NOTLOGIN"){
          wx.showToast({
            title: '登陆后才能使用该功能哦',
            icon:'none'
          })
          setTimeout(function() {
            // 1.3s后返回
            wx.switchTab({
              url: '/pages/users/users',
              success: function(res) {
                console.log('未登录，返回到个人页面')
              },
              fail: function(res) {},
              complete: function(res) {},
            })
          }, 1300);
        }else{
          this.setData({
          discussList:this.data.discussList.concat(res.data.data.records),
          // discussList:[...this.data.discussList,...res.data.data.records],
          pageSum:res.data.data.total
          })     
        }
          
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getComment()
  },
  onShow(){
    this.data.discussList=[]
    this.onLoad()
  },
  //展示下拉框
  bindShowMsg(){
    this.setData({
      unFold:!this.data.unFold
    })
  },
// 下拉框选择值
  mySelect(e){
    // console.log(e)
    let idx = e.currentTarget.dataset.index;
    this.setData({
      sortWay: this.data.array[idx],
      unFold:false,
      order:idx,
      discussList:[]
    })
    this.getComment()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.pageIndex * 4 >= this.data.pageSum){
      wx.showToast({
        title: '已经到底啦~',
        icon:'none'
      })
      return
    }
    if(!this.data.isShow) return
    this.setData({
      pageIndex: this.data.pageIndex+1
    })
    this.getComment()
  },
  gotoPublish(){
    wx.navigateTo({
      url: '/pages/comment/comment',
    })
  }
})